package com.hengxinguotong.zhihuichengjian.ui.device;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设备管理界面
 * Created by liux on 2017/3/7.
 */

public class DeviceListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.title)
    HXTextView title;
    @BindView(R.id.add)
    HXTextView add;
    private List<String> mTitleDataList;
    private ArrayList<BaseFragment> fragments;
    public static final String IN = "1";
    public static final String OUT = "2";
    public static final String ALL = "3";
    private DeviceListPagerAdapter deviceListPagerAdapter;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicelist);
        ButterKnife.bind(this);
        title.setText("设备进出列表");
        if (hasAdd()) {
            add.setVisibility(View.VISIBLE);
        }
        type = getIntent().getStringExtra("type");
        initView();

    }

    private void initView() {
        viewPager.setOffscreenPageLimit(3);
        fragments = new ArrayList<BaseFragment>();
        fragments.add(DeviceListFragment.newInstance(ALL, 1));
        fragments.add(DeviceListFragment.newInstance(IN, 1));
        fragments.add(DeviceListFragment.newInstance(OUT, 1));
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(deviceListPagerAdapter);


        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("全部");
        mTitleDataList.add("进场");
        mTitleDataList.add("出场");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_adadad));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_ffc065));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_ffc065));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(DeviceListActivity.this, 40);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
//                switch (position) {
//                    case 0:
//                        add.setVisibility(View.GONE);
//                        break;
//                    case 1:
//                    case 2:
//                        if(hasAdd()){
//                            add.setVisibility(View.VISIBLE);
//                        }
//                        break;
//                }
            }
        });

        if (DeviceInOutActivity.TYPE_DEVICE_OUT.equals(type)) {
            viewPager.setCurrentItem(2);
        } else if (DeviceInOutActivity.TYPE_DEVICE_IN.equals(type)) {
            viewPager.setCurrentItem(1);
        } else {
            viewPager.setCurrentItem(0);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        showToast("onNewIntent");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            for (BaseFragment baseFragment : fragments) {
                DeviceListFragment deviceListFragment = (DeviceListFragment) baseFragment;
                deviceListFragment.reLoad();
            }
            showToast("onActivityResult");
        }
    }

    @OnClick({R.id.iv_back, R.id.add})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.add:
                intent = new Intent(DeviceListActivity.this, DeviceInOutActivity.class);
                switch (viewPager.getCurrentItem()) {
                    case 1:
                        intent.putExtra("type", DeviceInOutActivity.TYPE_DEVICE_IN);
                        break;
                    case 2:
                        intent.putExtra("type", DeviceInOutActivity.TYPE_DEVICE_OUT);
                        break;
                }
                startActivity(intent);
                break;
        }
    }
}
